package cn.bsuc.crm.service.impl;

import cn.bsuc.crm.entity.Module;
import cn.bsuc.crm.entity.Permission;
import cn.bsuc.crm.mapper.ModuleMapper;
import cn.bsuc.crm.mapper.PermissionMapper;
import cn.bsuc.crm.service.ModuleService;
import cn.bsuc.crm.util.MenuTreeModel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuoyuan
 * @since 2022-10-03
 */
@Service
public class ModuleServiceImp extends ServiceImpl<ModuleMapper, Module> implements ModuleService {

    @Override
    public List<MenuTreeModel> getAllMenuTree(Integer roleId) {
        List<MenuTreeModel> list = new ArrayList<>();
        List<MenuTreeModel> all = baseMapper.getAll(roleId);
        //所有菜单（不是树状）
        for (MenuTreeModel m : all) {
            if (m.getParentId() == -1) {
                MenuTreeModel m1 = new MenuTreeModel();
                //给一级菜单属性赋值
                m1.setId(m.getId());
                m1.setParentId(m.getParentId());
                m1.setModuleName(m.getModuleName());
                m1.setOptValue(m.getOptValue());
                m1.setUrl(m.getUrl());
                //二级菜单
                List<MenuTreeModel> m2 = new ArrayList<>();
                //再次循环遍历所有菜单 all ，找出二级菜单
                for (MenuTreeModel em : all) {
                    if (em.getParentId() == m1.getId()) {
                        MenuTreeModel menu = new MenuTreeModel();
                        menu.setId(em.getId());
                        menu.setParentId(em.getParentId());
                        menu.setModuleName(em.getModuleName());
                        menu.setOptValue(em.getOptValue());
                        menu.setUrl(em.getUrl());
                        //三级菜单
                        List<MenuTreeModel> m3 = new ArrayList<>();
                        for (MenuTreeModel sm : all) {
                            if (sm.getParentId() == menu.getId()) {
                                m3.add(sm);
                            }
                        }
                        menu.setChildren(m3);
                        m2.add(menu);
                    }
                }
                m1.setChildren(m2);
                list.add(m1);
            }
        }
        return list;
    }

    @Override
    public List<Integer> seleteId(Integer roleId) {
        List<MenuTreeModel> all = baseMapper.getAll(roleId);
        List<Integer> list = new ArrayList<>();
        for (MenuTreeModel i : all) {
            if (i.getParentId() != -1) {
                list.add(i.getId());
            }
        }
        return list;
    }

    @Override
    public int saveMoule(Module module, Integer id) {
        System.out.println(id);
        if (module.getId() == null) {
            if (id == null) {
                module.setParentId(-1);
                int insert = baseMapper.insert(module);
                return insert;
            } else {
                module.setParentId(id);
                int insert = baseMapper.insert(module);
                return insert;
            }
        } else {
            int i = baseMapper.updateById(module);
            return i;
        }
    }

    @Override
    public int deleteModule(Integer id) {
        int i1 = baseMapper.deleteById(id);
        QueryWrapper<Module> moduleQueryWrapper = new QueryWrapper<>();
        moduleQueryWrapper.eq("parent_id", id);
        int delete = baseMapper.delete(moduleQueryWrapper);
        if (i1==1&&delete>=0){
            return 1;
        }
        return 0;
    }

}